package com.abase.site.client.prive.widget.login;

import java.util.Date;

import com.abase.site.client.PocAbase;
import com.abase.site.client.PocAbaseHistoryHandler;
import com.abase.site.client.PocAbaseHistoryHandler.HistoryTokens;
import com.abase.site.client.events.LoginRequestEvent;
import com.abase.site.client.events.LoginRequestHandler;
import com.abase.site.client.events.LoginResponseEvent;
import com.abase.site.shared.AbaseException;
import com.abase.site.shared.AbaseProfile;
import com.abase.site.shared.constants.AbaseConstants;
import com.abase.site.shared.login.LoginRequest;
import com.abase.site.shared.login.LoginResponse;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AbaseLoginManager extends Composite {

	private static final long UN_JOUR = 86400000L;
	private static AbaseLoginManagerUiBinder uiBinder = GWT
			.create(AbaseLoginManagerUiBinder.class);

	interface AbaseLoginManagerUiBinder extends
			UiBinder<Widget, AbaseLoginManager> {
	}
	
	private AbaseProfile profile = null;
	
	@UiField Label titleLabel;
	@UiField Label userLabel;
	@UiField Label mdpLabel;
	
	@UiField TextBox userTextBox;
	@UiField PasswordTextBox mdpTextBox;
	
	@UiField Label errMsg;
	
	@UiField Button validButton;
	@UiField Button annulButton;

	public AbaseLoginManager() {
		initWidget(uiBinder.createAndBindUi(this));
		this.redraw();
	}
	
	public AbaseProfile getProfile() {
		return this.profile;
	}
	
	private void redraw() {
		this.titleLabel.setText(AbaseLoginManagerMessages.INSTANCE.getTitle());
		this.userLabel.setText(AbaseLoginManagerMessages.INSTANCE.getTitle());
		this.mdpLabel.setText(AbaseLoginManagerMessages.INSTANCE.getMdpLabel());
		this.validButton.setText(AbaseLoginManagerMessages.INSTANCE.getValidButton());
		this.annulButton.setText(AbaseLoginManagerMessages.INSTANCE.getAnnulButton());
	}

	@UiHandler({"validButton","annulButton" })
	public void onClick(ClickEvent event) {
		if (event.getSource() == this.validButton) {
			doLogin(false);
		} else if (event.getSource() == this.annulButton) {
			PocAbase.getEventBus().fireEvent(new LoginResponseEvent());
		}
	}
	
	public void doLogin (final Boolean isSimpleCheck) {
		
		final LoginRequest request = new LoginRequest();
		
		if (isSimpleCheck) {
			request.setSimpleCheck(true);
		} else {
			request.setLogin(this.userTextBox.getText());
			request.setPassword(this.mdpTextBox.getText());
		}
		
		AsyncCallback<LoginResponse> callback = new AsyncCallback<LoginResponse>() {
			
			@Override
			public void onSuccess(LoginResponse result) {					
				if (null != result && null != result.getCookie()) {
					Cookies.setCookie(AbaseConstants.ABASEAUTH, result.getCookie(), new Date(System.currentTimeMillis() + UN_JOUR));
					AbaseLoginManager.this.profile = result.getAbaseProfile();
					History.newItem(HistoryTokens.PRIVATE_HOME_TOKEN.getValue());
				} else if (request.isSimpleCheck()) {
					LoginResponseEvent responseEvent = new LoginResponseEvent();
					responseEvent.getContext().setProfile(result.getAbaseProfile());
					responseEvent.getContext().setEmail(result.getAbaseProfile().getEmail());
					PocAbase.getEventBus().fireEvent(responseEvent);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				AbaseLoginManager.this.errMsg.setVisible(true);
				if (caught instanceof AbaseException) {
					switch (((AbaseException) caught).getAbaseExcpetionCodes()) {
						case ABASE_AUTH_BAD_LOGIN:
							AbaseLoginManager.this.errMsg.setText("bad login");
							break;
						case ABASE_AUTH_BAD_PASSWORD:
							AbaseLoginManager.this.errMsg.setText("bad passord");
							break;
						default:
							AbaseLoginManager.this.errMsg.setText("erreur");
					}
				}
			}
			
		};
		PocAbase.getAbaseServices().doLogin(request, callback);
			
	
	}
}
